package models

import (
	"errors"
	"github.com/beego/beego/v2/client/orm"
	"github.com/beego/beego/v2/core/logs"
	_ "github.com/go-sql-driver/mysql"
)

type IpccFile struct {
	Id       int64 `orm:"description(主键);pk;auto"`
	Path     string
	Direcory string
}

type IpccVoiceAudio struct {
	Id       int64
	PathInfo *IpccFile `orm:"description(文件Id);column(file_id);rel(one)"`
}

type IpccIvrManage struct {
	Id     int64  `orm:"description(主键);pk;auto"`
	AppId  int    `orm:"description(appId,企业Id);"`
	Name   string `orm:"description(名称)"`
	Active bool   `orm:"description(生效状态，默认生效);default(true);"`
	Number string `orm:"description(服务号码);column(fixed_number);null"`
	Time   string `orm:"description(服务时间段 星期);column(time);null"`
	Week   string `orm:"description(服务时间段 小时);column(week);null"`
	//Number *IpccFixedNumber `orm:"description(服务号码);rel(fk);null"`
}

type IpccIvrNodeType struct {
	Id    int    `orm:"description(主键);pk;auto"`
	Name  string `orm:"description(名称:开始、按键节点、放音节点、收号节点、判断节点、接口节点、转坐席节点、转外线节点、结束节点)"`
	Value string `orm:"description(字符串枚举:start,getkey,play,getDtmf,judge,api,toAgent,callNumber,end)"`
}
type IpccIvrNeighbour struct {
	Id        int64  `orm:"description(主键);pk;auto"`
	Node      string `orm:"description(节点Id);column(node_id)"`
	Child     string `orm:"description(子节点Id);column(next_node_id)"`
	ManagerId int    `orm:"description(流程Id)"`
	Key       string `orm:"description(关键字，表达式,为空则默认跳转);column(keystoke);null"`
}

type IpccIvrBasic struct {
	Id          int64           `orm:"description(主键);pk;auto"`
	AppId       string          `orm:"description(企业Id)"`
	OutOfWork   *IpccVoiceAudio `orm:"description(下班语音);null;rel(fk)"`
	LeavMessage *IpccVoiceAudio `orm:"description(留言语音);null;rel(fk)"`
	ErrKey      *IpccVoiceAudio `orm:"description(按键错误语音);null;rel(fk)"`
	HoldVoice   *IpccVoiceAudio `orm:"description(保持语音);null;rel(fk)"`
	PlayAgentId bool            `orm:"description(是否播报工号);default(false);null"`
}

type IpccIvrNode struct {
	Id        int64  `orm:"description(主键);pk;auto"`
	Name      string `orm:"description(节点名称);"`
	NodeId    string `orm:"description(节点Id);"`
	ManagerId int64  `orm:"description(流程Id)"`
	//NodeType  *IpccIvrNodeType `orm:"description(节点类型);rel(fk)"`
	NodeType  string `orm:"description(节点类型)"`
	PlayType  string `orm:"description(放音类型:tts,file)"`
	Tts       string `orm:"description(tts文字);null"`
	FileId    int64  `orm:"description(导航音Id);column(audio_id);null"`
	GetKey    bool   `orm:"description(是否获取按键);null"`
	Transfer  string `orm:"description(转接外线);column(to_number);null"`
	ToQueue   int64  `orm:"description(转接技能组Id);null"`
	ToService string `orm:"description(转坐席Id);null"`
	ApiUrl    string `orm:"description(接口节点URL);null"`
}

func GetFilePath(fileId int64) string {

	result := &IpccVoiceAudio{}
	o := orm.NewOrm()
	err := o.QueryTable(new(IpccVoiceAudio)).Filter("Id", fileId).RelatedSel("PathInfo").One(result)
	if err != nil {
		logs.Error("查询文件信息 %v 失败:%v", fileId, err)
		return ""
	}

	return result.PathInfo.Path
}

func LoadIvrMange(appId string) ([]IpccIvrManage, error) {

	logs.Debug("加载企业[%v] ivr 信息... ", appId)

	res := []IpccIvrManage{}
	o := orm.NewOrm()
	n, err := o.QueryTable(new(IpccIvrManage)).
		Filter("AppId", appId).
		Filter("Active", true).
		All(&res)
	if n == 0 || err != nil {
		logs.Error("加载ivr 失败:%v ", err)
		return nil, errors.New("EOF")
	}

	return res, nil
}

func LoadIvrNodes(manageId int64, offset int) ([]*IpccIvrNode, error) {

	var res []*IpccIvrNode

	o := orm.NewOrm()
	n, err := o.QueryTable(new(IpccIvrNode)).Filter("ManagerId", manageId).
		//RelatedSel("NodeType").
		//RelatedSel("FileInfo").
		Offset(offset * 1000).
		Limit(1000).
		All(&res)
	if n == 0 {
		return res, errors.New("EOF")
	}

	return res, err
}

func LoadIvrNodesNeighbour(manageId int64, offset int) ([]*IpccIvrNeighbour, error) {

	var res []*IpccIvrNeighbour

	o := orm.NewOrm()
	n, err := o.QueryTable(new(IpccIvrNeighbour)).Filter("ManagerId", manageId).
		Offset(offset * 1000).
		Limit(1000).
		All(&res)
	if n == 0 {
		return res, errors.New("EOF")
	}

	return res, err
}

func RegisterIvrs() {
	orm.RegisterModel(new(IpccIvrManage), new(IpccIvrNodeType), new(IpccIvrBasic), new(IpccIvrNeighbour), new(IpccIvrNode))
	orm.RegisterModel(new(IpccFile), new(IpccVoiceAudio))
}
